<?php

App::uses('AppController', 'Controller');

class AdminController extends AppController {
    
    public $layout    = 'admin';
    public $pageTitle = 'MRVICA Administracija';
    
    public $uses = array('User', 'Settings');
    
    public $components = array( 'Session', 
        'Auth' => array(
            'loginAction' => array(
                'controller' => 'admin',
                'action'     => 'login'
            )
        )
    );
    
    
    public function beforeRender() {
        parent::beforeRender();
    
        // set Main Menu
        $menu = array(
            'Podešavanja' => '/admin/index',
            'O nama'      => '/admin_pages/edit/o_nama',
            'Ukusi'       => '/admin_ukusi',
            'Proizvodi'   => '/admin_proizvodi',
            'Cene'        => '/admin_cene',
            'Odjava'      => '/admin/logout'
        );
        $this->set('menu', $menu);
    }
    
    public function login() {
        if ($this->Auth->loggedIn()) {
            $this->redirect('index');
        }
        
        $this->layout = 'login';
        
        if ($this->request->is('post')) {
            $data = $this->data['User'];
            
            if (! $this->Auth->login()) {
                $this->setFlash('Prijava nije uspela');
            }
            
            $this->redirect('index');
        }
    }
    
    public function logout() {
        $this->redirect($this->Auth->logout());
    }
    
    public function layout() {
        $this->render(false);
    }
    
    public function index() {
        if ($this->request->is('post')) {
            $data = $this->data;
            if ($this->Settings->saveThemAll($data['Settings'])) {
                $this->setFlash('Sačuvano.', 'ok');
            } else {
                $this->setFlash('Greška pri čuvanju podataka.');
            }
        }
    }
    
    public function errorHeader($message) {
        $this->response->header("errorMessage: {$message}");
        $this->response->header("HTTP/1.0 500 {$message}", true, 500);
    }
}